//
//  AskTableViewCell.m
//  Qyer
//
//  Created by dllo on 15/10/24.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "AskTableViewCell.h"

@implementation AskTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:18];
        self.titleLabel.numberOfLines = 2;
        [self.contentView addSubview:self.titleLabel];
        
        self.contentLabel = [[UILabel alloc] init];
        self.contentLabel.font = [UIFont systemFontOfSize:15];
        self.contentLabel.textColor = [UIColor lightGrayColor];
        self.contentLabel.numberOfLines = 2;
        [self.contentView addSubview:self.contentLabel];
        
        self.author_timeLabel = [[UILabel alloc] init];
        self.author_timeLabel.font = [UIFont systemFontOfSize:14];
        self.author_timeLabel.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:self.author_timeLabel];
        
        self.ask_answerLabel = [[UILabel alloc] init];
        self.ask_answerLabel.textAlignment = NSTextAlignmentRight;
        self.ask_answerLabel.font = [UIFont systemFontOfSize:14];
        self.ask_answerLabel.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:self.ask_answerLabel];
        self.date = [NSDate date];
        
    }
    return self;
}
- (void)setCellWithModel:(AskModel *)model text:(NSString *)searchText {
    self.attributedStr = [self illuminatedString:model.title rangeStr:searchText];
    _height = [StringWithHeight HeightWithText:model.title width:SCREEN_WIDTH - 40 * WIDTH font:[UIFont systemFontOfSize:18]];
    _titleLabel.frame = CRM(20 * WIDTH, 10, SCREEN_WIDTH - 40 * WIDTH, _height);
    _titleLabel.text = model.title;
    _titleLabel.attributedText = self.attributedStr;
    
    CGFloat height = [StringWithHeight HeightWithText:model.content width:self.titleLabel.frame.size.width font:self.contentLabel.font];
    if (height > 20) {
        height = 40;
    }
    self.attributedStr = [self illuminatedString:model.content rangeStr:searchText];
    _contentLabel.frame = CRM(20 * WIDTH, self.titleLabel.frame.size.height + 20, self.titleLabel.frame.size.width, height);
    _contentLabel.text = model.content;
    _contentLabel.attributedText = self.attributedStr;
    
    _author_timeLabel.frame = CRM(20 * WIDTH, self.contentLabel.frame.origin.y + height +10, 180, 20);
    NSString *string;
    int i = _date.timeIntervalSince1970 - model.add_time.integerValue;
    if (i < 60) {
        string = @"刚刚";
    } else if (i < 60 * 60) {
        string = [NSString stringWithFormat:@"%d分钟前", i / 60];
    } else if (i < 24 * 60 * 60) {
        string = [NSString stringWithFormat:@"%d小时前", i / 60 / 60];
    } else if (i < 24 * 60 * 60 * 7) {
        string = [NSString stringWithFormat:@"%d天前", i / 60 / 60 / 24];
    } else {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.add_time.integerValue];
        string = [formatter stringFromDate:date];
    }
    _author_timeLabel.text = [NSString stringWithFormat:@"%@ | %@", model.author, string];
    
    _ask_answerLabel.frame = CRM(SCREEN_WIDTH - 140, self.author_timeLabel.frame.origin.y, 120, 20);
    _ask_answerLabel.text = [NSString stringWithFormat:@"同问: %ld, 回答: %ld", model.ask_num, model.answer_num];
    self.height = self.height + height + 50;
    
}
- (NSAttributedString *)illuminatedString:(NSString *)text rangeStr:(NSString *)str {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange range = [text rangeOfString:str];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    
    return [attributedStr copy];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
